//
//  Fraction.m
//  Oc03_属性
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

#pragma mark - init
- (instancetype)initWithNumberator:(NSInteger)numberator denominator:(NSInteger)denominator{
    self = [super init];
    if (self) {
        _numberator = numberator;
        _denominator = denominator;
    }
    return self;
}
#pragma mark - 构造器
+ (instancetype)fractionWithNumberator:(NSInteger)numberator denominator:(NSInteger)denominator{
    return [[Fraction alloc] initWithNumberator:numberator denominator:denominator];
}
//+ (Fraction *)fracMinuWithFraction:(Fraction *)frac{
//    return [[Fraction alloc]mulWithFraction:frac];
//}
#pragma mark - 功能

//加法
-(Fraction *)plusWithFraction:(Fraction *)frac{
//fenzi
    NSInteger newNum = self.numberator * frac.denominator + self.denominator *frac.numberator;
    //funmu
    NSInteger newDeno = self.denominator * frac.denominator;
    //创建一个分数对象
    Fraction *newFrac = [Fraction fractionWithNumberator:newNum denominator:newDeno];
    
    //返回分数
    return newFrac;
}
//减
-(Fraction *)minusWithFraction:(Fraction *)frac{
    NSInteger newNum = self.numberator * frac.denominator - self.denominator * frac.numberator;
    NSInteger newDemo = self.denominator * frac.denominator;
    Fraction *newFrac = [Fraction fractionWithNumberator:newNum denominator:newDemo];
    return newFrac;
}

//乘
-(Fraction *)mulWithFraction:(Fraction *)frac{
    NSInteger newNum = self.numberator * frac.numberator;
    NSInteger newDemo = self.denominator * frac.denominator;
    Fraction *newFrac = [Fraction fractionWithNumberator:newNum denominator:newDemo];
    return newFrac;
    
}
//除
- (Fraction *)devidWithFraction:(Fraction *)frac{
    NSInteger newNum = self. numberator * frac.denominator;
    NSInteger newDemo = self.denominator * frac.numberator;
    Fraction *newFrac = [Fraction fractionWithNumberator:newNum denominator:newDemo];
    return newFrac;
}
//比较大小
- (Fraction *)compareWithFraction:(Fraction *)frac{
    
    NSInteger newNum1 = self.numberator * frac.denominator;
    NSInteger newNum2 = self.denominator * frac.numberator;
    //Fraction   = newNum1 > newNum2 ? newNum1 :newNum2;
    if (newNum1 > newNum2) {
        Fraction *newFrac = [Fraction fractionWithNumberator:self.numberator denominator:self.denominator];
        return newFrac;
    }
    else {
        Fraction *newFrac = [Fraction fractionWithNumberator:frac.numberator denominator:frac.denominator];
        return newFrac;
    }
}
//yue
- (Fraction *)yueWithFraction{
    NSInteger max = self.numberator > self.denominator ? self.numberator :self.denominator;
    NSInteger min = self.numberator < self.denominator ? self.numberator :
    self.denominator;
    NSInteger yue = max % min ;
    while(yue != 0)
    {max = min;
     min = yue;
     yue = max % min;
    }//此时当yue = 0时候,最大公约数是Min
    if (yue != 0) {
        
        Fraction *newFrac = [Fraction fractionWithNumberator:self.numberator /yue denominator:self.denominator /yue];
        return newFrac;
    }
    else  {
        Fraction *newFrac = [Fraction fractionWithNumberator:self.numberator /min denominator:self.denominator /min];
        return newFrac;
    }
}


@end
